package pl.lodz.p.services;

import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;

import pl.smsapi.Client;
import pl.smsapi.api.SmsFactory;
import pl.smsapi.api.action.sms.SMSSend;
import pl.smsapi.api.response.MessageResponse;
import pl.smsapi.api.response.StatusResponse;
import pl.smsapi.exception.ActionException;
import pl.smsapi.exception.ClientException;
import pl.smsapi.exception.HostException;
import pl.smsapi.exception.SmsapiException;

public class NotificationSendServiceImpl implements NotificationSendService{

	static MimeMessage generateMailMessage;
	static Session getMailSession;
	static Properties mailServerProperties;
	
	 private static String USER_NAME = "grzegorzwyrwas10";  // GMail user name (just the part before "@gmail.com")
	 private static String PASSWORD = "776h3566"; // GMail password
	    
	public String getSendEmail(String emailAddress, String subject, String body) {
		return sendMail(emailAddress, subject, body);
		
	}
	
	
	public String sendMail(String emailAddress, String subject, String body) {
		try {
			mailServerProperties = System.getProperties();
			mailServerProperties.put("mail.smtp.port", "587");
			mailServerProperties.put("mail.smtp.auth", "true");
			mailServerProperties.put("mail.smtp.starttls.enable", "true");
			System.out.println("Mail Server Properties have been setup successfully..");
	 
	//Step2		
			System.out.println("\n\n 2nd ===> get Mail Session..");
			getMailSession = Session.getDefaultInstance(mailServerProperties, null);
			generateMailMessage = new MimeMessage(getMailSession);
				generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress));
			generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(emailAddress));
			generateMailMessage.setSubject(subject);
			String emailBody = "Test email by Crunchify.com JavaMail API example. " + "<br><br> Regards, <br>Crunchify Admin";
			generateMailMessage.setContent(body, "text/html");
			System.out.println("Mail Session has been created successfully..");
	 
	//Step3		
			System.out.println("\n\n 3rd ===> Get Session and Send mail");
			Transport transport = getMailSession.getTransport("smtp");
			
			// Enter your correct gmail UserID and Password (XXXApp Shah@gmail.com)
			transport.connect("smtp.gmail.com", USER_NAME, PASSWORD);
			transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
			transport.close();
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "fail";
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "fail";
		}
		return "success";
	}

	public String sendSms(String texts, String phoneNumbers){
		try {
		    Client client = new Client("grzegorzwyrwas10@gmail.com");
		    client.setPasswordHash("9806935e7fc06c34fef325a1f5b4fa72");

		    SmsFactory smsApi = new SmsFactory(client);
		    
			    SMSSend action = smsApi.actionSend()
			            .setText(texts)
			            .setTo(phoneNumbers)
			            .setSender("Recepcja"); 
			    StatusResponse result = action.execute();
			    for(MessageResponse status : result.getList() ) {
			        System.out.println( status.getNumber() + " " + status.getStatus() );
		    }
		} catch( ActionException e) {
		    System.out.println(e.getMessage());
            return "fail";
		} catch( ClientException e) {
		    System.out.println(e.getMessage());
            return "fail";
		} catch( HostException e) {
		    System.out.println(e.getMessage());
            return "fail";
		} catch( SmsapiException e ) {
		    System.out.println(e.getMessage());
            return "fail";
		} catch (Exception e) {
            return "fail";
		}
		return "success";
	}
}
